package com.atung.parking.service.impl;

import com.atung.parking.config.auth.AuthUserMangerContext;
import com.atung.parking.entity.BusMenu;
import com.atung.parking.enums.AdminType;
import com.atung.parking.mapper.BusMenuMapper;
import com.atung.parking.response.page.OneselfMenuResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
public class MenuManageServiceImpl {

    @Resource
    private BusMenuMapper busMenuMapper;

    public List<OneselfMenuResponse> queryOneselfService() {
        AdminType roleType = AuthUserMangerContext.getAuthUserEntity().getRoleType();

        QueryWrapper<BusMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_type", roleType.ordinal());
        List<OneselfMenuResponse> busMenus = busMenuMapper.selectList(queryWrapper).stream()
                .map(item->OneselfMenuResponse.builder()
                        .sortIndex(item.getSortIndex())
                        .menuName(item.getMenuName())
                        .id(item.getId())
                        .parentId(item.getParentId())
                        .children(new ArrayList<>())
                        .build()
                ).collect(Collectors.toList());
        return buildTree(busMenus);

    }


    public static List<OneselfMenuResponse> buildTree(List<OneselfMenuResponse> menus) {
        // 使用 Map 存储所有菜单项，以便快速查找
        Map<Integer, OneselfMenuResponse> menuMap = menus.stream()
                .collect(Collectors.toMap(OneselfMenuResponse::getId, Function.identity()));

        // 存储根节点
        List<OneselfMenuResponse> rootMenus = new ArrayList<>();

        for (OneselfMenuResponse menu : menus) {

            if (menu.getParentId().equals("-1")) {
                // 如果 parentId 为 -1，表示这是根节点

                rootMenus.add(menu);
            } else {
                // 否则，找到父节点并添加到父节点的 children 列表中
                OneselfMenuResponse parentMenu = menuMap.get(Integer.valueOf(menu.getParentId()));
                if (parentMenu != null) {
                    if (parentMenu.getChildren() == null) {
                        parentMenu.setChildren(new ArrayList<>());
                    }
                    parentMenu.getChildren().add(menu);
                }

            }
        }

        // 对每个节点的子节点进行排序
       return rootMenus.stream().sorted(Comparator.comparingInt(OneselfMenuResponse::getSortIndex))
                .collect(Collectors.toList());

    }

    private static void sortChildren(OneselfMenuResponse menu) {
        if (menu.getChildren() != null) {
            menu.setChildren(menu.getChildren().stream()
                    .sorted(Comparator.comparingInt(OneselfMenuResponse::getSortIndex))
                    .collect(Collectors.toList()));
            menu.getChildren().forEach(MenuManageServiceImpl::sortChildren);
        }
    }
}
